/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat.MsgEvent;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author spider391Tang
 */
public class ChatClient extends EventTransceiver {

    private PrintWriter out = null;
    public ChatClient(Socket sock,PrintWriter out) {
        super(sock);
        this.out = out;
    }

    EventObject receiveEvent() throws IOException {
        EventObject ev = null;
        try {
            String rawMsg = in.readLine();
            System.out.println("==>" + rawMsg);
            String[] names = rawMsg.split(" ");
            String msgType = names[0];
            /// 公子乖 -　Parse 參數
            if (msgType.equals("/show")) {
                ev = new ShowEvent(rawMsg.substring(5), ShowEvent.NEWLINE);
            } else if (msgType.equals("/LoginOK")) {
                ev = new LoginOKEvent(rawMsg.substring(9));
            } else if (msgType.equals("/obj")) {
                // /obj yoyo chat.CircleBean 10 10
                String cmd = "";
                for (int i = 1; i < names.length; i++) {
                    cmd += names[i];
                    cmd += " ";
                }
                ev = new DrawBeanEvent(cmd);
            } else if (msgType.equals("/move")) {
                String cmd = "";
                for (int i = 1; i < names.length; i++) {
                    //cmd + = (names[i] + " ");
                    cmd += names[i];
                    cmd += " ";
                }
                ev = new MoveBeanEvent(cmd);
            } else if (msgType.equals("/change")) {
                String cmd = "";
                for (int i = 1; i < names.length; i++) {
                    cmd += names[i];
                    cmd += " ";
                }
                ev = new ChangeBeanEvent(cmd);
            }else if(msgType.equals("/kick")){
                ev = new KickUserEvent(names[1]);
            }else if(msgType.equals("/msg")){
                ev =  new MsgPostEvent(rawMsg.substring(names[0].length()+1));
            }else if(msgType.equals("/post")){
                ev =  new PostEvent(rawMsg.substring(names[0].length()+1));
            }


            //ev = (EventObject)oin.readObject();
            //System.out.println(ev);
        } catch (ClassCastException e) {
            System.out.println("Non-event object sent to EventTransceiver");
        }

        return ev;
    }
    public void tell_server_start()
    {
        out.println("/client_ready");
    }

   
}

